package cn.edu.jxau.core.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.edu.jxau.core.controller.ServiceResultConfig;
import cn.edu.jxau.core.exception.ServiceException;
import cn.edu.jxau.core.util.DateUtils;
import cn.edu.jxau.core.util.StringUtil;
import cn.edu.jxau.framework.dao.core.Session;
import cn.edu.jxau.framework.dao.core.sqlSessionFactory;

public class HardwareService extends ServiceSupport{
    private final String ERROR_MSG = "数据异常";
    private final String HARDWARE_MSG = "hardware_msg";
	
    public HardwareService() {
	}

    public HardwareService(String view, Map<String, Object[]> params, Map<String, Object> data) throws ServiceException {
        super(view, params, data);
        System.out.println("comming");
        execute(view, params, data); // 执行业务
    }
    
    
	/**
	 * 添加硬件
	 * 
	 * */
	@SuppressWarnings("unused")
	private void addHardware(Map<String, Object[]> params, Map<String, Object> data){
        Map<String, Object> param = new HashMap<String, Object>(); //用来封装传递给数据库的参数
        List<HashMap<String, Object>> datas = new ArrayList();  // 用来接收数据库返回回来的 

        String hardwareType = getParamStringValue(params, "hardwareType", 0);
        String hardwareName = getParamStringValue(params, "hardwareName", 0);
        String spectrumTypeID = getParamStringValue(params, "spectrumTypeID", 0);

        //获取当前时间
        Date addTime=DateUtils.getMonthOne();
       
        //数据库操作
        
        //插入数据
        param.put("hardwareType", hardwareType);
        param.put("hardwareName", hardwareName);
        param.put("spectrumTypeID", spectrumTypeID);
        param.put("addTime", addTime);
        
        Session session=null;
        try {
            session = sqlSessionFactory.getInstance().getSession();
            session.execute("addHardware", param, datas);
            if(null == hardwareName || null == spectrumTypeID){
            	data.put("result",FAILED);
            }
            else{
            	 data.put("result",SUCCESS);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
        	//释放会话
            session.releaseConnection();
        }
	}
	
	
	/**
	 * 删除硬件
	 * 
	 * */
	@SuppressWarnings("unused")
	private void deleteHardware(Map<String, Object[]> params, Map<String, Object> data){
        Map<String, Object> param = new HashMap<String, Object>(); //用来封装传递给数据库的参数
        List<HashMap<String, Object>> datas = new ArrayList();  // 用来接收数据库返回回来的 
        
        String hardwareID = getParamStringValue(params, "hardwareID", 0);

        //数据库操作
        
        //删除操作
        param.put("hardwareID", hardwareID);
        
        Session session=null;
        try {
            session = sqlSessionFactory.getInstance().getSession();
            session.execute("deleteHardware", param, datas);  
            if(null == hardwareID){
            	data.put("result",FAILED);
            }
            else{
            	 data.put("result",SUCCESS);
            }
            
          
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
        	  //释放会话
            session.releaseConnection();
        }
	}
	
	/**
	 * 修改硬件
	 * 
	 * */
	@SuppressWarnings("unused")
	private void updateHardware(Map<String, Object[]> params, Map<String, Object> data){
        Map<String, Object> param = new HashMap<String, Object>(); //用来封装传递给数据库的参数
        List<HashMap<String, Object>> datas = new ArrayList();  // 用来接收数据库返回回来的 
        
        String hardwareID = getParamStringValue(params, "hardwareID", 0);
        String hardwareType = getParamStringValue(params, "hardwareType", 0);
        String hardwareName = getParamStringValue(params, "hardwareName", 0);
        
        //修改操作
        param.put("hardwareType", hardwareType);
        param.put("hardwareName", hardwareName);
        param.put("hardwareID", hardwareID);
        
        Session session=null;
        try {
            session = sqlSessionFactory.getInstance().getSession();
            session.execute("updateHardware", param, datas);
            if(null == hardwareID){
            	data.put("result",FAILED);
            }
            else{
            	 data.put("result",SUCCESS);
            }
            data.put("result", SUCCESS);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
        	//释放会话
            session.releaseConnection();
        }
	}
	
	/**
	 * 查询硬件
	 * 
	 * */
	@SuppressWarnings("unused")
	private void queryHardware(Map<String, Object[]> params, Map<String, Object> data){
        Map<String, Object> param = new HashMap<String, Object>(); //用来封装传递给数据库的参数
        List<HashMap<String, Object>> datas = new ArrayList();  // 用来接收数据库返回回来的 
        
        
        Session session=null;
        try {
            session = sqlSessionFactory.getInstance().getSession();
            session.execute("queryHardware", param, datas);

            
            if(null == datas || datas.isEmpty()){
            	data.put("result", FAILED);
            }
            else{
            	//保存session的数据
            	data.put("session_hardware", datas);
                data.put("result", SUCCESS);
        		for (Map.Entry<String, Object> entry : data.entrySet()) {  
      			  
        		    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());  
        		}  
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
        	//释放会话
            session.releaseConnection();
        }
	}

}
